package main

import (
	"fmt"
	"time"
	"github.com/pmlpml/rxgo"
)

func main() {
	fmt.Println("Debounce:")
	rxgo.Just(0, 12, 12, 7, 12, 34).Map(func(x int) int {
		switch x {
		case 0:
			time.Sleep(0 * time.Millisecond)
		case 12:
			time.Sleep(1 * time.Millisecond)
		case 7:
			time.Sleep(2 * time.Millisecond)
		case 34:
			time.Sleep(3 * time.Millisecond)
		default:
			time.Sleep(1 * time.Millisecond)
		}
		return x
	}).Debounce(2 * time.Millisecond).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(",")
	})
	fmt.Println()
  
	fmt.Println("Distinct:")
	rxgo.Just(0, 12, 7, 34, 2, 0).Distinct().Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(",")
	})
	fmt.Println()

	fmt.Println("ElementAt:")
	rxgo.Just(0, 12, 7, 34, 2).ElementAt(2).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()

	fmt.Println("First:")
	rxgo.Just(0, 12, 7, 34, 2).First().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()

	fmt.Println("IgnoreElements:")
	rxgo.Just(0, 12, 7, 34, 2).IgnoreElements().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()

	fmt.Println("Last:")
	rxgo.Just(0, 12, 7, 34, 2).Last().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()

	fmt.Println("Skip:")
	rxgo.Just(0, 12, 7, 34, 2).Skip(3).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(",")
	})
	fmt.Println()

	fmt.Println("SkipLast:")
	rxgo.Just(0, 12, 7, 34, 2).SkipLast(3).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(",")
	})
	fmt.Println()

	fmt.Println("Take:")
	rxgo.Just(0, 12, 7, 34, 2).Take(4).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(",")
	})
	fmt.Println()

	fmt.Println("TakeLast:")
	rxgo.Just(0, 12, 7, 34, 2).TakeLast(3).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(",")
	})
	fmt.Println()

	fmt.Println("Sample:")
	observableP := make(chan interface{})
	go func() {
		rxgo.Just(1, 2).Map(func(x int) int {
			switch x {
			case 1:
				time.Sleep(10 * time.Millisecond)
			case 2:
				time.Sleep(5 * time.Millisecond)
			default:
				time.Sleep(10 * time.Millisecond)
			}
			return x
		}).Subscribe(func(x int) {
			observableP <- x
		})
	}()
	rxgo.Just(0, 12, 7, 34, 2).Map(func(x int) int {
		time.Sleep(5 * time.Millisecond)
		return x
	}).Sample(observableP).Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
}
